package com.kingdee.eas.custom.wg.basedata.app;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.wg.basedata.PatternBaseCollection;
import com.kingdee.eas.custom.wg.basedata.PatternBaseFactory;
import com.kingdee.eas.custom.wg.basedata.PatternBaseInfo;
import com.kingdee.eas.custom.wg.basedata.client.BillstatusEnum;
import com.kingdee.eas.framework.Result;
import com.kingdee.eas.util.app.DbUtil;

public class PatternBaseControllerBean extends AbstractPatternBaseControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.wg.basedata.app.PatternBaseControllerBean");

	protected Result _save(Context ctx, IObjectCollection colls)
			throws BOSException, EASBizException {
		// TODO Auto-generated method stub
		return super._save(ctx, colls);
	}

	protected IObjectPK _save(Context ctx, IObjectValue model)
			throws BOSException, EASBizException {
		// TODO Auto-generated method stub
		return super._save(ctx, model);
	}


	protected IObjectValue _actionAudit(Context ctx, IObjectValue model)
			throws BOSException {
		// TODO Auto-generated method stub
		PatternBaseInfo info=(PatternBaseInfo)model;
		info.setBillstatus(BillstatusEnum.audit);
		Date date=new Date();
		info.setAuditDate(date);
		try {
			PatternBaseFactory.getLocalInstance(ctx).update(new ObjectUuidPK(info.getId().toString()), info);
		} catch (EASBizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BOSException(e.getMessage());
		}
		return info;
	}


	protected IObjectValue _actionUnaudit(Context ctx, IObjectValue model)
			throws BOSException {
		// TODO Auto-generated method stub
		PatternBaseInfo info=(PatternBaseInfo)model;
		info.setBillstatus(BillstatusEnum.unAudit);
		Date date=new Date();
		info.setAuditDate(null);
		try {
			PatternBaseFactory.getLocalInstance(ctx).update(new ObjectUuidPK(info.getId().toString()), info);
		} catch (EASBizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BOSException(e.getMessage());
		}
		return info;
	}


	protected IObjectCollection _acionUnauditBatch(Context ctx,
			IObjectCollection models) throws BOSException {
		// TODO Auto-generated method stub
		PatternBaseCollection col=(PatternBaseCollection) models;
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		for(int i=0; i<col.size() ;i++ ){
			PatternBaseInfo info=	(PatternBaseInfo)(col.get(i));
			String sql=" update T_BAS_PatternBase set FBillstatus ='"+BillstatusEnum.UNAUDIT_VALUE+"' " +
					",FAuditDate = null where fid='"+info.getId().toString()+"'";
			try {
				
				DbUtil.execute(ctx, sql);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new BOSException(e.getMessage());
			}
		}
		
		return null;
	}


	protected IObjectCollection _actionAuditBatch(Context ctx,
			IObjectCollection models) throws BOSException {
		// TODO Auto-generated method stub
		PatternBaseCollection col=(PatternBaseCollection) models;
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		for(int i=0; i<col.size() ;i++ ){
			PatternBaseInfo info=	(PatternBaseInfo)(col.get(i));
			String sql=" update T_BAS_PatternBase set FAuditDate =to_date('"+sf.format(date)+"'), FBillstatus ='"+BillstatusEnum.AUDIT_VALUE+"' where fid='"+info.getId().toString()+"'";
			try {
				DbUtil.execute(ctx, sql);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new BOSException(e.getMessage());
			}
		}
		return null;
	}
}